package com.weiyun.notebook

import android.animation.ObjectAnimator
import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    val MODE_LINEAR = 0
    val MODE_GRID = 1

    val KEY_LAYOUT_MODE = "key_layout_mode"

    private var currentListLayoutMode = MODE_LINEAR
    private lateinit var noteViewModel: NoteViewModel
    private lateinit var mMyAdapter:MyAdapter
    private var mAnimator: ObjectAnimator? = null
    private var mPlayer: MediaPlayer? = null
    var isPause=false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initPlayer()
        noteViewModel=ViewModelProvider(this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        ).get(NoteViewModel::class.java)
        noteViewModel.allNotes.observe(this, Observer { notes->
            //更新ui
            mMyAdapter = MyAdapter(this, notes,noteViewModel)
//            //设置布局显示格式
            rlv.layoutManager = LinearLayoutManager(this@MainActivity)
            rlv.adapter = mMyAdapter
        })
    }

    private fun initPlayer() {
        mPlayer = MediaPlayer.create(this, R.raw.note)
        mPlayer!!.isLooping = true //设置为循环播放
        mPlayer!!.start() //开始播放音乐
        mAnimator = ObjectAnimator.ofFloat(iv1, "rotation", 0.0f, 360.0f)
        mAnimator?.duration = 3000 //设定转一圈的时间
        mAnimator?.repeatCount = Animation.INFINITE //设定无限循环
        mAnimator?.repeatMode = ObjectAnimator.RESTART // 循环模式
        mAnimator?.interpolator = LinearInterpolator() // 匀速
        mAnimator?.start() //动画开始
    }

    fun add(view: View?) {
        val intent = Intent(this, AddActivity::class.java)
        startActivity(intent)
    }
    fun stop(view: View?){
        isPause = if (!isPause) {
//            iv_pause.setBackgroundResource(R.drawable.iv_pause)
            mAnimator!!.resume() //动画继续
            mPlayer!!.start() //继续开始音乐播放
            true
        } else {
//            iv_pause.setBackgroundResource(R.drawable.iv_start)
            mAnimator!!.pause() //动画暂停
            mPlayer!!.pause() //暂停音乐播放
            false
        }
    }
    /**
     * 模糊查询
     */
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        val searchView = menu.findItem(R.id.menu_search).actionView as SearchView
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                return false
            }
            override fun onQueryTextChange(newText: String): Boolean {
                noteViewModel.getSpeNotes(newText).observe(this@MainActivity, Observer { notes->
                    mMyAdapter= MyAdapter(this@MainActivity,notes,noteViewModel)
                    rlv.layoutManager = LinearLayoutManager(this@MainActivity)
                    rlv.adapter = mMyAdapter
                })

                return true
            }
        })
        return super.onCreateOptionsMenu(menu)
    }
    private fun setToLinearList() {
        val linearLayoutManager: RecyclerView.LayoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rlv.layoutManager = linearLayoutManager
        mMyAdapter.setViewType(mMyAdapter.TYPE_LINEAR_LAYOUT)
        mMyAdapter.notifyDataSetChanged()
    }


    private fun setToGridList() {
        val gridLayoutManager: RecyclerView.LayoutManager = GridLayoutManager(this, 2)
        rlv.layoutManager = gridLayoutManager
        mMyAdapter.setViewType(mMyAdapter.TYPE_GRID_LAYOUT)
        mMyAdapter.notifyDataSetChanged()
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        item.isChecked = true
        return when (item.itemId) {
            R.id.stop -> {
                isPause = if (!isPause) {
//            iv_pause.setBackgroundResource(R.drawable.iv_pause)
                    mAnimator!!.resume() //动画继续
                    mPlayer!!.start() //继续开始音乐播放
                    true
                } else {
//            iv_pause.setBackgroundResource(R.drawable.iv_start)
                    mAnimator!!.pause() //动画暂停
                    mPlayer!!.pause() //暂停音乐播放
                    false
                }
                true
            }
            R.id.menu_linear -> {
                setToLinearList()
                currentListLayoutMode = MODE_LINEAR
//                SpfUtil.saveInt(this, KEY_LAYOUT_MODE, MODE_LINEAR)
                true
            }
            R.id.menu_grid -> {
                setToGridList()
                currentListLayoutMode = MODE_GRID
//                SpfUtil.saveInt(this, KEY_LAYOUT_MODE,MODE_GRID)
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }



}